//
//  ZTTypeConvert.m
//  ZHYYTool
//
//  Created by MT-SW-005 on 2021/9/17.
//  Copyright © 2021 MT-SW-005. All rights reserved.
//

#import "ZTTypeConvert.h"
#import "ZTRegex.h"
@implementation ZTTypeConvert

/* NSString --> NSData*/
+(NSData*)stringToData:(NSString*)string
{
    if (string == nil) {
        return nil;
    }
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    return data;
}

/*NSData -->NSString*/
+(NSString*)dataToString:(NSData*)data
{
    if (data == nil) {
        return nil;
    }
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}


/* NSString  NSDictorary*/
+(NSDictionary*)jsonStringToDictonary:(NSString*)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


+(NSString*)dictToString:(NSDictionary*)dict
{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:0];
    NSString *dataStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return dataStr;
}

/* NSArray NSData*/
+(NSData*)arrayToData:(NSArray*)array
{
    if (array == nil) {
        return  nil;
    }
//    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array requiringSecureCoding:NO error:nil];
    return data;
}

+(NSArray*)dataToArray:(NSData*)data
{
    if (data == nil) {
           return  nil;
    }
//    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSArray *array = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:data error:nil];
    return array;
}

/* NSDictorary NSData*/
+(NSData*)dictToData:(NSDictionary*)dict
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict requiringSecureCoding:NO error:nil];
    return data;
}

+(NSDictionary*)dataToDict:(NSData*)data
{
    NSDictionary *dict = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] fromData:data error:nil];
    return dict;
}


+(id)jsonToDictOrArray:(NSString*)json
{
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    if (data) {
           return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    }else {
        return nil;
    }
}
        

+(NSString*)objToJson:(id)obj
{
    NSString *json = @"";
    if  ([NSJSONSerialization   isValidJSONObject:obj]) {
        NSData  *data = [NSJSONSerialization   dataWithJSONObject:obj  options: NSJSONWritingPrettyPrinted   error:nil ];
         json = [[NSString   alloc] initWithData:data  encoding:NSUTF8StringEncoding ];
         NSLog(@"%@",json);
    }
    return json;
}




/*字符串转换为数组 没有分割符 "abc" -->[a,b,c] */
+ (NSMutableArray*)stringToArray:(NSString*)str
{
    NSMutableArray *strArray = [NSMutableArray array];
    NSString *tmp = nil;
      for(int i = 0; i < [str length]; i++) {
          tmp = [str substringWithRange:NSMakeRange(i, 1)];
          [strArray addObject:tmp];
      }
    return strArray;
}


/* 字符串按照分割符转换为数组*/
+(NSArray *)stringToArray:(NSString*)aString WithSplit:(NSString *)split
{
    NSArray *array = [aString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:split]];
    return array;
}


/* 通过正则匹配自动字符串转换为数组
    "str1;str2,str3 str4" -->[str1,str2,str3,str4]
 */
+ (NSArray *)autoStringToArray:(NSString *)aString
{
    NSArray *matchArray = [ZTRegex matchesInString:aString expressionPattern:@"\\w+"];
    return matchArray;
}



/* 数组合并为字符串
*  数组中包含，字符串，NSNumber,char需要转换为字符串存储在数组
*/
+(NSString *)arrayToString:(NSArray*)array
{
    NSMutableString *stringData = [NSMutableString string];
    for(int i=0; i<[array count]; i++){
        [stringData appendFormat:@"%@",array[i] ];
    }
    return stringData;
}


/*
把数组合并为字符串，通过分隔符连接
*/
+(NSString *)joinStringFormArray:(NSArray*)arr byString:(nullable NSString*)split{
    NSString *str;
    if (split ==nil) {
        str = [arr componentsJoinedByString:@""];
    }else{
        str = [arr componentsJoinedByString:split];
    }
    return str;
}


/*将分割的字符串通过分隔符合并 */
+ (NSString *)joinString:(NSString *)aString withSplitString:(NSString *)SplitString
{
    NSString *resultString = [aString stringByReplacingOccurrencesOfString:SplitString withString:@""];
    return resultString;
    
}
/* 通过正则匹配自动合并分割字符串*/
+ (NSString *)autoJoinString:(NSString *)aString
{
    NSMutableString *resultString = [NSMutableString string];
    for (int i=0; i<aString.length; i++) {
        NSString *subString = [aString substringWithRange:NSMakeRange(i, 1)];
        NSArray *matchArray = [ZTRegex matchesInString:subString expressionPattern:@"\\w+"];
        if (matchArray.count >0) {
            [resultString appendString:subString];
        }
    }
    return resultString;
}


+(NSString*)reverseString:(NSString *)aString //FFFFFFFFFFFFFFF7
{
    NSUInteger length = [aString length];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:length];

    for(long i=length; i>0; i--)
    {
        unichar c = [aString characterAtIndex:i-1];
        [array addObject:[NSString stringWithFormat:@"%c",c]];
    }
    NSMutableString *str = [NSMutableString stringWithCapacity:length];
    for(int i=0; i<length; i++)
    {
        [str appendString:array[i]];
    }
    return [NSString stringWithString:str];
 }


/*
 C语言字符串转化成OC字符串
 */
+(NSString*)charToString:(char*)cStr
{
    NSString *str = [NSString stringWithCString:cStr encoding:NSUTF8StringEncoding];
    return str;
}


/*
 OC语言字符串转化成C字符串
 */
+ (const char*)stringToChar:(NSString*)aString
{
   const char *cStr = [aString cStringUsingEncoding:NSUTF8StringEncoding];
    return cStr;
}
   

@end
